import 'dart:ffi';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/generated/l10n.dart';
import 'package:full_display/provide/cmd_provider.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/socket/udp/socket_udp_utils.dart';
import 'package:full_display/unit/tool_fun.dart';
import 'package:full_display/unit/web_socket/web_sockect_utils.dart';
import 'package:provider/provider.dart';

class SliderWidget extends StatefulWidget {
  final String title;
  final String state;
  final double minValue;
  final double maxValue;
  final int sliderValue;
  const SliderWidget(
      {required this.title,
      required this.state,
      required this.minValue,
      required this.maxValue,
      required this.sliderValue,
      Key? key})
      : super(key: key);

  @override
  State<SliderWidget> createState() => _SliderWidgetState();
}

class _SliderWidgetState extends State<SliderWidget> {
  late double _sliderValue = widget.sliderValue.toDouble();
  late double _runSliderValue = 60.0;
  String sliderText = "还没操作";
  late double _minValue;
  late double _maxValue;
  late String _state;
  late CMDProvider _cmdData;
  @override
  Widget build(BuildContext context) {
    // if (widget.state == 'run') {
    //   _sliderValue = 60.0;
    // }
    _cmdData = context.watch<CMDProvider>();
    _minValue = widget.minValue;
    _maxValue = widget.maxValue;
    _state = widget.state;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.only(left: 20.0.W, top: 20.0.H),
          child: Text(
            widget.title,
            style: StandardTextStyle.textWhite12Style,
          ),
        ),
        Container(
          padding: EdgeInsets.only(top: 10.0.H),
          height: 30,
          child: _slider(),
        ),
        Container(
          padding: EdgeInsets.only(left: 30.0.W, right: 30.0.W),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                S.current.di,
                style: StandardTextStyle.textWhite12Op50Style,
              ),
              Text(
                S.current.gao,
                style: StandardTextStyle.textWhite12Op50Style,
              ),
            ],
          ),
        ),
      ],
    );
  }

  // SliderTheme _sliderTheme(context) {
  //   return SliderTheme(
  //     data: SliderThemeData(
  //       trackHeight: 20,
  //       activeTrackColor: ColorPlate.sub_blue,
  //       inactiveTrackColor: ColorPlate.backgroundOpacity20,
  //       overlayShape: RoundSliderOverlayShape(),
  //       valueIndicatorColor: Colors.red,
  //       showValueIndicator: ShowValueIndicator.onlyForDiscrete,
  //       minThumbSeparation: 100,
  //       rangeTrackShape: RoundedRectRangeSliderTrackShape(),
  //     ),
  //     child: Slider(
  //       value: sliderValue,
  //       min: 0,
  //       max: 100,
  //       onChanged: (value) {
  //         print("SliderTheme onChanged : $value");
  //         updateSlider(value, "onChanged : $value");
  //       },
  //       onChangeStart: (value) {},
  //     ),
  //   );

  Slider _slider() {
    return Slider(
      value: widget.state == 'run' ? _runSliderValue : _sliderValue,
      activeColor: ColorPlate.sub_blue,
      inactiveColor: ColorPlate.backgroundOpacity20,
      min: _minValue,
      max: _maxValue,
      divisions: widget.state != 'run' ? _maxValue.toInt() : 30,
      onChanged: (value) {
        updateSlider(value, "onChanged : $value");
      },
      onChangeStart: (value) {
        updateSlider(value, "onChangeStart : $value");
      },
      onChangeEnd: (value) {
        updateSlider(value, "onChangeEnd : $value");

        if (_state == 'run') {
          WebSocketUtils().sendMessage(35, content: value * 10);
        } else if (_state == 'light') {
          _cmdData.deviceSettingInfo['properties']['brightness'] =
              value.toInt();
          WebSocketUtils().sendMessage(7, content: value);
        } else if (_state == 'voice') {
          _cmdData.deviceSettingInfo['properties']['volume'] = value.toInt();
          WebSocketUtils().sendMessage(11, content: value);
        } else if (_state == 'udp_light') {
          SocketUDPUtils()
              .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:HILIGHT");
          // SocketUDPUtils()
          //     .sendUDPMsgToIp(InternetAddress.anyIPv4, 123456, "CMD:LOWLIGHT");
        }
      },
    );
  }

  void updateSlider(value, text) {
    // print("onChangeEnd : $value");
    if (widget.state == 'run') {
      _runSliderValue = value;
    } else {
      _sliderValue = value;
    }

    sliderText = text;
    setState(() {});
  }
}
